package com.kingyea.app.market.supplierAllocation.service.impl;

import com.alibaba.fastjson.JSON;
import com.kingyea.app.market.area.controller.AreaController;
import com.kingyea.app.market.market.bean.Market;
import com.kingyea.app.market.supplierAllocation.bean.SupplierAllocation;
import com.kingyea.app.market.supplierAllocation.dao.SupplierAllocationMapper;
import com.kingyea.app.market.supplierAllocation.service.SupplierAllocationService;
import com.kingyea.app.market.utils.page.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version v1.0.0
 * @description SupplierAllocation 实现类
 * @date Created in 2018-02-09 17:48:58
 */
@Service
@Transactional
public class SupplierAllocationServiceImpl implements SupplierAllocationService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SupplierAllocationServiceImpl.class);

    @Resource
    private SupplierAllocationMapper supplierAllocationMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public SupplierAllocation getById(Integer id) throws Exception {
        return this.supplierAllocationMapper.getById(id);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public Object list(Map<String, Object> supplierAllocation) throws Exception {
        PageInfo.startPage(supplierAllocation);
        return PageInfo.getPageInfo(supplierAllocationMapper.list(supplierAllocation));
    }

    /**
     * 列表(不分页)
     *
     * @param
     * @return
     */
    @Override
    public List<Object> listNoPage(Map<String, Object> supplierAllocation) throws Exception {
        return supplierAllocationMapper.list(supplierAllocation);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) throws Exception {
        supplierAllocationMapper.deleteById(id);
        return 1;
    }

    /**
     * 保存
     *
     * @param supplierAllocation
     * @return
     */
    @Override
    public void save(List<Object> list) {
        supplierAllocationMapper.save(list);
    }

    /**
     * 更新
     *
     * @param supplierAllocation
     * @return
     */
    @Override
    public SupplierAllocation update(SupplierAllocation supplierAllocation) throws Exception {
        try {
            supplierAllocation.setCreateDate(new Date(System.currentTimeMillis()));
            supplierAllocationMapper.update(supplierAllocation);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return supplierAllocation;
    }

    @Override
    public Object getUserList(Map<String, Object> supplier) throws Exception {
        PageInfo.startPage(supplier);
        return PageInfo.getPageInfo(supplierAllocationMapper.getUserList(supplier));
    }

    @Override
    public int deleteUser(String supplierCode, String loginAccount) {
        supplierAllocationMapper.deleteUser(supplierCode, loginAccount);
        return 1;
    }

    @Override
    public Object getMarketList(Map<String, Object> supplierCode) throws Exception {
        PageInfo.startPage(supplierCode);
        //根据code查出区域所有父级对象
        List<Object> marketList = supplierAllocationMapper.getMarketList(supplierCode);
        for (int i = 0; i < marketList.size(); i++) {
            Market market = (Market) marketList.get(i);
            List areaLsit = JSON.parseObject(String.valueOf(redisTemplate.opsForValue().get(AreaController.area_details_prefix + market.getAdminArea())), List.class);
            if (null != areaLsit && areaLsit.size() > 0) {
                if (areaLsit.get(0).toString().contains("中国")) {
                    areaLsit.remove(0);
                }
            }
            market.setAdminArea(JSON.toJSONString(areaLsit));
        }
        return PageInfo.getPageInfo(marketList);
    }

    @Override
    public int deleteMarket(String supplierCode, String marketCode) {
        supplierAllocationMapper.deleteMarket(supplierCode, marketCode);
        return 1;
    }

    @Override
    public int deleteSupplier(String supplierCode, String linkSupplier) {
        supplierAllocationMapper.deleteSupplier(supplierCode, linkSupplier);
        return 1;
    }

    @Override
    public Object getLinkSupplier(@RequestBody Map<String, Object> supplierCode) throws Exception {
        PageInfo.startPage(supplierCode);
        return PageInfo.getPageInfo(supplierAllocationMapper.getLinkSupplier(supplierCode));
    }

    @Override
    public List<SupplierAllocation> getBySupplierCode(Integer supplierCode) {

        List<SupplierAllocation> bySupplierCode = null;
        try {

            bySupplierCode = supplierAllocationMapper.getBySupplierCode(supplierCode);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return bySupplierCode;
    }

    @Override
    public List<SupplierAllocation> getByMarket(String marketCode) throws Exception {
        // TODO Auto-generated method stub
        List<SupplierAllocation> byMarket = supplierAllocationMapper.getByMarket(marketCode);
        return byMarket;
    }

}
